﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace Btrieve_Retriever
{
    static class Program
    {
        public static String myConnectionString;
        public static OdbcConnection myConnection;
        public static SqlConnection myConnection2;
        public static SqlConnection myConnection3;
        static int mode = 0;
        public static StreamWriter logWriter;

        /// <summary>
        /// The main entry point for the application. Sets up the two database connections.
        /// One for the ODBC, the other for SQL. 
        /// </summary>
        [STAThread]
        static void Main(string [] args)
        {
            if (args != null && args.Length > 0)
            {
                mode = int.Parse(args[0]);
            }
            try
            {
                Application.EnableVisualStyles();

                myConnectionString = "Dsn=NieseDb;servername=192.168.80.17.1583;serverdsn=niesedb;arrayfetchon=0;arraybuffersize=8;transporthint=TCP:SPX;decimalsymbol=,;clientversion=9.10.020.000;codepageconvert=1252;autodoublequote=0";
                //myConnectionString = "Driver={Pervasive ODBC Client Interface};servername=192.168.80.17;dbq=@niesedb;";
                myConnection = new OdbcConnection(myConnectionString);
                myConnection.Open();

                string path = "";
                if (mode == 0)
                {
                    path = Path.Combine(Environment.CurrentDirectory, @"ConnectionSettings.txt");
                }
                else
                {
                    String argPath = args[1];
                    path = argPath.Replace("\\", "\\\\");
                    String logPath = path + "\\Log.txt";
                    path += "\\ConnectionSettings.txt";

                    logWriter = new StreamWriter(logPath, false);
                    logWriter.WriteLine("Application started in batch mode");
                }
                StreamReader sr = new StreamReader(path);
                Console.WriteLine("PATH; " + path);
                string line;
                string connectionStringSetting = "";
                while((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine ("Con from file: " + line);
                    connectionStringSetting = line.Trim();
                }

                myConnection2 = new SqlConnection(connectionStringSetting);
                myConnection2.Open();

                myConnection3 = new SqlConnection(connectionStringSetting);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error setting up connections. \nCheck the ConnectionSettings text file\n\n\nDetails: " + ex.Message);
                Environment.Exit(0);
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
            Application.Run(new HomeDisplay(mode));
        }

        /* On closing the application, connections must be terminated
         * */
        private static void Application_ApplicationExit(object sender, EventArgs e)
        {
            Console.WriteLine("CLOSING");
            if (myConnection.State == ConnectionState.Open)
            {
                myConnection.Close();
            }
            if (myConnection2.State == ConnectionState.Open)
            {
                myConnection2.Close();
            }
            if (myConnection3.State == ConnectionState.Open)
            {
                myConnection3.Close();
            }
            if (logWriter != null)
            {
                if (logWriter.BaseStream != null)
                {
                    logWriter.Close();
                }
            }
        }

        /* On closing the application, connections must be terminated
         * */
        private static void CurrentDomain_ProcessExit(object sender, EventArgs e)
        {
            Console.WriteLine("CLOSING");
            if (myConnection.State == ConnectionState.Open)
            {
                myConnection.Close();
            }
            if (myConnection2.State == ConnectionState.Open)
            {
                myConnection2.Close();
            }
            if (myConnection3.State == ConnectionState.Open)
            {
                myConnection3.Close();
            }
        }
    }
}
